package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.JobApplyService;
import com.lanchetech.admin.service.JobService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ContentUploadPictureResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.AreaVO;
import com.lanchetech.bean.vo.JobApplyVo;
import com.lanchetech.entity.Job;
import com.lanchetech.entity.JobApply;
import com.lanchetech.entity.LogisticsCompany;
import com.lanchetech.service.AreaService;
import com.lanchetech.service.FileService;
import com.lanchetech.service.LogisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(value = "通用接口", tags = "例如文件上传等")
@RequestMapping("/api/common")
@RestController
public class CommonController {

    @Autowired
    FileService fileService;

    @Autowired
    AreaService areaService;

    @Autowired
    LogisticsService logisticsService;

    @Autowired
    JobService jobService;

    @Autowired
    JobApplyService jobApplyService;

    @ApiOperation("上传文件，包括图片等")
    @PostMapping("/uploadFile")
    public ResultData<String> uploadFile(@RequestParam("file") MultipartFile file,
                                         @RequestParam(value = "oldImage", required = false, defaultValue = "") String oldImage) {
        return fileService.uploadFile(file, oldImage);
    }

    @ApiOperation("上传富文本图片")
    @PostMapping("/updateContentPicture")
    public ContentUploadPictureResp updateContentPicture(@RequestParam("file") MultipartFile file) {
        return fileService.updateContentPicture(file);
    }

    @ApiOperation("获取省市区街道列表")
    @GetMapping("/getAreaList")
    public ResultData<List<AreaVO>> getAreaList(@RequestParam("level") Integer level, @RequestParam(value = "pid", required = false) Long pid) {
        return areaService.getAreaList(level, pid);
    }

    @ApiOperation("获取所有快递公司列表")
    @GetMapping("/getLogisticsCompanyList")
    public ResultData<List<LogisticsCompany>> getLogisticsCompanyList() {
        return logisticsService.getLogisticsCompanyList();
    }

    @OperatorLog("编辑岗位")
    @ApiOperation("编辑岗位")
    @PreAuthorize("hasAuthority('user.post.edit.auth')")
    @PostMapping("/editJob")
    public BaseResp editJob(@RequestBody Job job) {
        return jobService.editJob(job);
    }

    @ApiOperation("获取岗位详情")
    @GetMapping("/getJobDetail")
    public ResultData<Job> getJobDetail(@RequestParam("id") Long id) {
        return jobService.getJobDetail(id);
    }

    @OperatorLog("删除岗位")
    @ApiOperation("删除岗位")
    @PreAuthorize("hasAuthority('user.post.edit.auth')")
    @GetMapping("/deleteJob")
    public BaseResp deleteJob(@RequestParam("id") Long id) {
        return jobService.deleteJob(id);
    }

    @ApiOperation("获取岗位分页")
    @PostMapping("/getJobPage")
    @PreAuthorize("hasAuthority('user.post')")
    public ResultData<BasePageResp<Job>> getJobPage(@RequestBody PageReq req) {
        return jobService.getJobPage(req);
    }

    @ApiOperation("获取应聘岗位分页")
    @PostMapping("/getJobApplyPage")
    @PreAuthorize("hasAuthority('user.apply-post')")
    public ResultData<BasePageResp<JobApplyVo>> getJobApplyPage(@RequestBody PageReq req) {
        return jobApplyService.getJobApplyPage(req);
    }
}
